package com.gdit.zax.servletjsp;


import java.io.File;
import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.dao.VideoDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.entitys.VideoInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class Sys_Delvideo extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		/zuiaixue/sys_delvideo
//		http://pingkgs3pc:8080/zuiaixue/servlet_listmvideo?page=1&onepagecount=10
		
		//==============================获取参数
		String token = webUtil.getCookie(request, "LoginCookie");
		int delid = TextUtil.String2int(request.getParameter("id"), -1);
		
		
		//==============================检测参数
		if (delid < 1) {
			Repost.message("请输入正确的参数", request, response);
			return;
		}
		
		//==============================检测数据库、权限
		Connection conn = DBUtil.getConnection();
		if (conn == null) {
			Repost.dberror(request, response);
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
		if (info==null) {
			Repost.nologin(response);
			return;
		}
		
		if (info.getUlevel()<Constant.ULEVEL_teacher) {
			Repost.message("权限不够!", request, response);
			return;
		}
		
		//==============================开始执行
		VideoDao vdao = dao.getVideoDao();
		VideoInfo vinfo = vdao.getvideoinfo(delid);
		if (vinfo == null) {
			Repost.message("需要删除的视频已被删除或不存在!", request, response);
			return;
		}
		File delvideo = new File(Constant.WEBPATH  +  vinfo.getUrl());
		delvideo.delete();
		
		File delpic = new File(Constant.WEBPATH +  vinfo.getPic());
		delpic.delete();
		
		if (vdao.delVideo(delid)) {
//			Repost.message("删除成功", request, response);
			Repost.back(request, response);
		} else {
			Repost.message("删除失败", request, response);
		}
		
		
		
		
	}

}
